﻿using System;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using MemoryGames.Games;
using System.Windows;

namespace MemoryGames
{
    /// <summary>
    /// Interaction logic for MainForm.xaml
    /// </summary>
    public partial class MainForm : Window
    {
		private bool isInMaintenanceMode = false;

        #region Delegates
        public delegate void MainFormRestartDelegate();
        #endregion

        #region Constructors
        public MainForm()
        {
            InitializeComponent();
            Initialize(false);
        }
        #endregion

        #region Methods
        /// <summary>
        /// Clear all controls from canvas exept background
        /// </summary>
        private void ClearControls()
        {
			MenuItemPanel.Children.Clear();
        }

        /// <summary>
        /// Clear screen and add gamestartbuttons
        /// </summary>
        /// <param name="maintenanceMode">Specify if application should load in maintenance mode</param>
        private void Initialize(bool maintenanceMode)
        {
			isInMaintenanceMode = maintenanceMode;

            //Clear all controls exept background
            ClearControls();

			// Add menu title
			Image title = new Image();
			title.Source = new BitmapImage(new Uri("/Resources/menu_title.png", UriKind.RelativeOrAbsolute));
        	title.Width = 759;
			MenuItemPanel.Children.Add(title);

			AddGameButton(GameType.WrongWord, "Verkeerde woord");
			if(maintenanceMode)
				AddGameButton(GameType.WrongWordMaintenance, "Vw onderhoud");
			AddGameButton(GameType.SearchDifferences, "Verschillen");
			if (maintenanceMode)
				AddGameButton(GameType.SearchDifferencesMaintenance, "Ver. onderhoud");
			AddGameButton(GameType.Memory, "Memory");
			if (maintenanceMode)
				AddGameButton(GameType.MemoryMaintenance, "Memory onderhoud");

			// Add circles effect
        	//MemoryGames.Controls.Circles circles = new MemoryGames.Controls.Circles();
        	//circles.CreateCircles(c);
        }

		/// <summary>
		/// Adds a game button with event handler to the form
		/// </summary>
		/// <param name="gameType"></param>
		/// <param name="description"></param>
		private void AddGameButton(GameType gameType, string description)
		{
			var b = new GameStartButton(gameType);
			b.GameSelected += new GameStartButton.GameSelectedDelegate(b_GameSelected);
			b.Text = description;
			b.Margin = new Thickness(0, 20, 0, 0);
			MenuItemPanel.Children.Add(b);
		}

        #endregion

        #region Event Handlers
        /// <summary>
        /// Event raised when game is selected
        /// </summary>
        /// <param name="game"></param>
		void b_GameSelected(GameType game)
        {
			UserControl gc = null;

            switch (game)
			{
				case GameType.WrongWord:
					gc = new MemoryGames.Games.WrongWord.Main();
					break;
			case GameType.WrongWordMaintenance:
					gc = new MemoryGames.Games.WrongWord.Maintenance();
                    break;
				case GameType.SearchDifferences:
					gc = new MemoryGames.Games.SearchDifferences.Main();
                    break;
				case GameType.SearchDifferencesMaintenance:
					gc = new MemoryGames.Games.SearchDifferences.Maintenance();
                    break;
				case GameType.Memory:
					gc = new MemoryGames.Games.Memory.Main();
					break;
				case GameType.MemoryMaintenance:
					gc = new MemoryGames.Games.Memory.Maintenance();
					break;
            }

			//GK Return if gametype is not recognized
			if (gc == null) return;

			//GK Add standard event handlers for game controls
			((IGameControl)gc).MainFormRestart += new MainFormRestartDelegate(f_MainFormRestart);

			// Initialize game control
			if (!((IGameControl)gc).Initialize()) return;
			
			//GK Add control to form
			ClearControls();
			MenuItemPanel.Children.Add(gc);
        }

        /// <summary>
        /// Receive event from gameform to restart mainform
        /// </summary>
        void f_MainFormRestart()
        {   Initialize(false);
        }

		/// <summary>
		/// Handle keypresses on main form
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if(e.Key == System.Windows.Input.Key.Tab)
			{
				// User hit F12, initialize application in maintenance mode
				Initialize(!isInMaintenanceMode);
			}
		}
        #endregion

    }
}
